package com.brightyoyo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * @author bri
 * @since 15-8-18.
 */
public class RightTriangleView extends View implements RightTriangleDrawer {

    private Paint mBgPaint;
    private Paint mTextPaint;
    private int mBgResId = Color.RED;
    private CharSequence mText = new String();

    public RightTriangleView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public RightTriangleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public RightTriangleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);

    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        initPaint();

        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.RightTriangleView);
        mText = ta.getString(R.styleable.RightTriangleView_android_text);
        mText = TextUtils.isEmpty(mText) ? "" : mText;
        mBgResId = ta.getColor(R.styleable.RightTriangleView_android_background, mBgResId);
        ta.recycle();

    }

    private void initPaint() {
        mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, getResources().getDisplayMetrics());
        int measureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
        setMeasuredDimension(measureSpec, measureSpec);
    }

    @Override protected void onDraw(Canvas canvas) {

        final float width = getMeasuredWidth();
        final float height = getMeasuredHeight();

        mBgPaint.setColor(mBgResId);

        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(width, 0);
        path.lineTo(0, height);
        path.close();
        canvas.drawPath(path, mBgPaint);

        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        final int textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, getResources().getDisplayMetrics());
        textPaint.setTextSize(textSize);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextAlign(Paint.Align.CENTER);

        canvas.drawText(mText, 0, mText.length(), width / 3, height / 2, textPaint);
    }

    @Override public void setBackgroundDrawable(Drawable background) {

    }

    @Override public void setTriangle(CharSequence text, @ColorRes int bgResId) {
        mBgResId = getResources().getColor(bgResId);
        mText = text;
        invalidate();
    }
}
